package com.neuedu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//com.neuedu.servlet.FirstServlet
public class FirstServlet  extends HttpServlet {


    private int num;

    /**
     * 初始化一次   第一次请求
     */
    public FirstServlet() {
        System.out.println("初始化 FirstServlet");
    }


    /**
     * 初始化
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        System.out.println("FirstServlet.init");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        num++;
        System.out.println(this);
        System.out.println("req = " + req.getClass());
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<h1>Hello Servlet</h1>");
        writer.flush();
        writer.close();
    }

    @Override
    public void destroy() {
        System.out.println("FirstServlet.destroy");
    }
}
